<?php


$arr_header=array();
$content_category=array();
$content_food=array();
$i_content_food=0;
$row = 1;
$category_col=0;
$category_name='';
$cols=3;

if (($handle = fopen("data2.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);   
        //if category,food name is empty or is filled,then strip it.
        if((!$data[0] && !$data[1])){
        	continue;
        }
        if(($data[0] && $data[1])){
        	$arr_header=$data;
        	$tmp_category_title=explodeName($data[0]);
        	$cols=isset($tmp_category_title[1]) && (int)$tmp_category_title[1]?(int)$tmp_category_title[1]:$cols;
        	continue;
        }
       // var_dump($cols);exit;
        //if category name
        if($data[0]){
        	$arr_category=explodeName($data[0]);
        	//echo '<pre>';print_r($arr_category);exit;
        	//$arr_categroy=explode('(',$data[0]);        	
        	$category_name=$arr_category[0];
        	//$category_col=$arr_category[1];
        	@$content_category[$category_name]=array();//[$category_col]
        }else{
        	if(!$category_name){
        		continue;
        	}
        	
        	@$content_category[$category_name][]=$i_content_food;//[$category_col]
        	$content_food[$i_content_food++]=$data;
        }
        
       /*  echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        } */
    }
    fclose($handle);
}
//$cols=max(array_keys($content_category));


/*
 echo '<pre>';print_r($content_category);
echo max(array_keys($content_category)); */
?>
<script type="text/javascript">
	var arr_content=<?php echo  json_encode($content_food);?>;
	var selected_list=[];
	var i=0,j=0,total=[];
	function itemClicked(index){
		if(document.getElementById('item'+index).checked){
			selected_list.push(index);
		}else{
			for(i=0,cnt=selected_list.length;i<cnt;i++){if(selected_list[i]==index){selected_list.splice(i,1);}}
		}

		total=[];
		for(i=0,cnt=selected_list.length;i<cnt;i++){
			total.push(arr_content[selected_list[i]][1]);		 
		}
		document.getElementById("total_1").innerHTML = total.join(',');

		for(i=2;i<10;i++){
			total=0;
			for(j=0,cnt=selected_list.length;j<cnt;j++){
				total+=parseFloat(arr_content[selected_list[j]][i]);				
			}
			document.getElementById("total_"+i).innerHTML = Math.round(total);
			//	Math.round(parseInt(document.getElementById("total_"+i).innerHTML,10)) +  Math.round(parseInt(arr_content[index][i],10));
		}
	}
</script>
 <form name="NutritionForm" id="NutritionForm" action="">
                    <table width="1000" cellspacing="0" cellpadding="5" border="0" align="center">
                        <tbody>
                        	<tr><td colspan="2">Nutritional Calculator</td></tr>
                        	
                            <tr>
                            <td style="width:700px;">
                            <table style="width:100%">
                            <?php 
                            	/* for($i=1;$i<=$cols;$i++){
                            		echo '<td  valign="top"><table>'; [$i]*/
                            		foreach((array)$content_category as $key_category=>$v_category){
                            			echo '<tr><th colspan="'.$cols.'" style="text-align: left;width:700px;">'.$key_category.'</th></tr><tr>';
                            			
                            			foreach((array)$v_category as $index=>$content_id){
                            				if($index && $index % $cols ==0){
                            					echo '</tr><tr>';
                            				}
                            				echo '<td><input type="checkbox" id="item'.$content_id.'" name="item'.$content_id.'" onclick="itemClicked('.$content_id.')"> '.$content_food[$content_id][1].'</td>';
                            			}
                            			if($index % $cols != $index-1){
                            				echo '<td colspan="'.($index - 1 - ($index % $cols)).'">&nbsp;</td>';
                            			}
                            			echo '</tr>';                            			
                            		}
                            		/* echo '</table></td>';
                            	} */
                            	
                            	
                            	
                            			
                            			?>
                            			</table></td>
                            			
                            			<td style="width:300px" id="nutritionTable"   valign="top">
										<table><tr><td  width="300px">
                        <!-- start display nutrition -->
                        <table align="right" cellspacing="0" cellpadding="0" border="0" style="border: 1px solid #000000; width: 250px; padding: 3px; font-size: 10px; background-color:#FFFFFF;">
                            <tbody>
                                <tr>
                                    <td colspan="2">
                                    <span style="font-size: 20px; font-weight: 900; letter-spacing: 2px; color:#000000;">Nutrition Facts</span></td>
                                </tr>
                                <tr>
                                    <td colspan="2" style="border-top: 7px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">Amount Per Serving</span>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">Calories <span id="total_2">0</span></span>
                                    </td>
                                    <td style="border-top: 1px solid #000000; text-align: right;">
                                    <span style="display:none">Cal from Fat <span id="caloriesfromfat">0</span></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" style="border-top: 4px solid #000000; text-align: right;">
                                    <span style="display:none"><span style="font-weight: 900;">% Daily Value*</span></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">Total Fat <span id="total_3">0</span>g</span>
                                    </td>
                                    <td style="border-top: 1px solid #000000; text-align: right;">
                                    <span style="display:none"><span style="font-weight: 900;" id="totalfatpercent">0</span>%</span>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" style="padding-left: 5px;"><span style="display:none">
                                    <table cellspacing="0" cellpadding="0" border="0" style="width: 235px; font-size: 11px;">
                                        <tbody>
                                            <tr>
                                                <td style="border-top: 1px solid #000000;">
                                                Saturated Fat <span id="saturatedfat">0g</span>
                                                </td>
                                                <td style="border-top: 1px solid #000000; text-align: right;">
                                                <span style="display:none"><span style="font-weight: 900;" id="saturatedfatpercent">0</span>%</span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="border-top: 1px solid #000000;">
                                                <span style="font-style: italic;">Trans</span> Fat <span id="transfat">0g</span>
                                                </td>
                                                <td style="border-top: 1px solid #000000; text-align: right;">&nbsp;
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">Cholesterol <span id="total_4">0</span>mg</span>
                                    </td>
                                    <td style="border-top: 1px solid #000000; text-align: right;">
                                    <span style="display:none"><span style="font-weight: 900;" id="cholesterolpercent">0</span>%</span>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">Sodium <span id="total_5">0</span>mg</span>
                                    </td>
                                    <td style="border-top: 1px solid #000000; text-align: right;">
                                    <span style="display:none"><span style="font-weight: 900;" id="sodiumpercent">0</span>%</span>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">Total Carbs <span id="total_6">0</span>g</span>
                                    </td>
                                    <td style="border-top: 1px solid #000000; text-align: right;">
                                    <span style="display:none"><span style="font-weight: 900;" id="totalcarbohydratepercent">0</span>%</span>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" style="padding-left: 10px;">
                                    <table cellspacing="0" cellpadding="0" border="0" style="width: 100%; font-size: 11px;">
                                        <tbody>
                                            <tr>
                                                <td style="border-top: 1px solid #000000;">
                                                <span style="font-weight: 900; font-size:14px; color:#000000;">Dietary Fiber <span id="total_7">0</span>g</span>
                                                </td>
                                                <td style="border-top: 1px solid #000000; text-align: right;">
                                                <span style="display:none"><span style="font-weight: 900;" id="diataryfiberpercent">0</span>%</span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="border-top: 1px solid #000000;">
                                                <span style="font-weight: 900; font-size:14px; color:#000000;">Sugars <span id="total_8">0</span>g</span>
                                                </td>
                                                <td style="border-top: 1px solid #000000; text-align: right;">&nbsp;
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">Protein <span id="total_9">0</span>g</span>
                                    </td>
                                    <td style="border-top: 1px solid #000000; text-align: right;">&nbsp;
                                    </td>
                                </tr>
                                <!-- <tr>
                                    <td style="border-top: 4px solid #000000;" colspan="2">
                                    <table cellspacing="0" cellpadding="0" border="0" style="width: 240px; font-size: 11px;">
                                        <tbody>
                                            <tr>
                                                <td>
                                                Vitamin A
                                                </td>
                                                <td>
                                                <span id="vitaminapercent">0</span>%
                                                </td>
                                                <td style="padding: 0pt 5px;">
                                                &bull;
                                                </td>
                                                <td>
                                                Vitamin C
                                                </td>
                                                <td>
                                                <span id="vitamincpercent">0</span>%
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="border-top: 1px solid #000000;">
                                                Calcium
                                                </td>
                                                <td style="border-top: 1px solid #000000;">
                                                <span id="calciumpercent">0</span>%
                                                </td>
                                                <td style="border-top: 1px solid #000000; padding: 0pt 5px;">
                                                &bull;
                                                </td>
                                                <td style="border-top: 1px solid #000000;">
                                                Iron
                                                </td>
                                                <td style="border-top: 1px solid #000000;">
                                                <span id="ironpercent">0</span>%
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid #000000;" colspan="2">
                                     <table cellspacing="0" cellpadding="0" border="0" style="width: 240px; font-size: 11px;">
                                        <tbody>
                                            <tr>
                                                <td style="text-align: center; vertical-align: top; width: 10px;">
                                                *
                                                </td>
                                                <td style="width: 230px;">
                                                Percent Daily Values are based on a 2,000 calorie diet. Your daily values may be higher or lower depending on your calorie needs.
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table> 
                                    </td>
                                </tr>-->
                                <tr>
                                    <td colspan="2" style="border-top: 4px solid #000000;">
                                    <span style="font-weight: 900; font-size:14px; color:#000000;">INGREDIENTS: </span><span style="font-size:14px; color:#000000;" id="total_1"></span>
                                    </td>
                                </tr>
                                
                            </tbody>
                        </table> 
                        </td></tr>
						<tr></td>
                        <table  style="text-align:right; padding-right:5px;width: 100%;padding-top: 10px;">
                                 <tr>
                                <td colspan="<?php echo $cols+1;?>" style="text-align:right; padding-right:45px;">
                                 <input name="submit" type="button" value=" Reset " onclick="window.location.href=window.location.href" />
                                </td>
                            </tr>
                               </table>  
							   </td></tr></table>
                        </td>
                        </tr>
                            
                           
                            </tbody>
                            </table>

</form>


<?php 
function explodeName($str){
	$arr_str=explode('(',$str);
	$append= isset($arr_str[1])?trim($arr_str[1],'()'):'';
	$name=$arr_str[0];
	return array($name,$append);
}

?>
